package com.yixun.lbx.controller;

import com.yixun.controller.BaseController;
import com.yixun.mapper.UserMapper;
import com.yixun.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/lbx")
public class sysController extends BaseController {
    @Resource
    private UserMapper userMapper;
    @GetMapping("/user/userInfo")
    public String goUserInfo(Model model){
        model.addAttribute("userInfo",userMapper.getUserBean(this.getSessionUser().getUser_id()));
        return "/lbx/user/userInfo";
    }
    @PostMapping("/user/changePassword")
    @ResponseBody
    public String changePassword(@NotNull String oldPassword, @NotNull String newPassword){
        Map<String,String> map = new HashMap<>();
        String s = new MD5Util().MD5(oldPassword);
        map.put("user_name",this.getSessionUser().getUser_name());
        map.put("password", s);
        if (userMapper.checkOldPw(map) != null){
            map.put("new_pw",new MD5Util().MD5(newPassword));
            map.put("user_id",this.getSessionUser().getUser_id());
            int i = userMapper.resetPassword(map);
            if (i==1){
                return "1";
            }else {
                return "-1";
            }
        }else {
            return "0";
        }
    }
}
